package com.ruoyi.duty.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.duty.domain.DutySdpc;
import com.ruoyi.duty.domain.bo.DutySdpcBo;
import com.ruoyi.duty.domain.vo.DutySdpcVo;
import com.ruoyi.duty.mapper.DutySdpcMapper;
import com.ruoyi.duty.service.IDutySdpcService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 涉毒排查Service业务层处理
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@RequiredArgsConstructor
@Service
public class DutySdpcServiceImpl implements IDutySdpcService {

    private final DutySdpcMapper baseMapper;

    /**
     * 查询涉毒排查
     */
    @Override
    public DutySdpcVo queryById(Long sdpcId) {
        return baseMapper.selectVoById(sdpcId);
    }

    /**
     * 查询涉毒排查列表
     */
    @Override
    public TableDataInfo<DutySdpcVo> queryPageList(DutySdpcBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DutySdpc> lqw = buildQueryWrapper(bo);
        Page<DutySdpcVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询涉毒排查列表
     */
    @Override
    public List<DutySdpcVo> queryList(DutySdpcBo bo) {
        LambdaQueryWrapper<DutySdpc> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DutySdpc> buildQueryWrapper(DutySdpcBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DutySdpc> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getPclx()), DutySdpc::getPclx, bo.getPclx());
        lqw.eq(bo.getPxsj() != null, DutySdpc::getPxsj, bo.getPxsj());
        return lqw;
    }

    /**
     * 新增涉毒排查
     */
    @Override
    public Boolean insertByBo(DutySdpcBo bo) {
        DutySdpc add = BeanUtil.toBean(bo, DutySdpc.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSdpcId(add.getSdpcId());
        }
        return flag;
    }

    /**
     * 修改涉毒排查
     */
    @Override
    public Boolean updateByBo(DutySdpcBo bo) {
        DutySdpc update = BeanUtil.toBean(bo, DutySdpc.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DutySdpc entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除涉毒排查
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
